package org.uds.android;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class Accelerometer extends Activity implements AccelerometerListener{
	private static Context CONTEXT;
	private String data;
	private static final int PERIOD = 1000;
	private static int time;
	private File f = null;
	 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	data = "";
    	time = 0;
    	try {
    	f = new File(Environment.getExternalStorageDirectory(), "p.txt");
    	FileWriter gpxwriter = new FileWriter(f);
        BufferedWriter out = new BufferedWriter(gpxwriter);
        out.write("Hello world");
        out.close();
    	} catch (Exception e) {
    		
    	}
    	/*
    	try{
    		File ff = new File(getExternalFilesDir(null), "p.txt");
     		OutputStream os = new FileOutputStream(ff);
     		os.write("It really works!".getBytes());
     		os.close();
    	//FileWriter ff = new FileWriter("/sdcard/fitdroid/example.txt");
		 //ff.write("It works!");
    	} catch (Exception e) {}
    	*/
    	/*
    	try {
    	    
    		File root = Environment.getExternalStorageDirectory();
    	    if (root.canWrite()){
    	        File gpxfile = new File(root, "gpxfile.gpx");
    	        
    	    }
    	} catch (IOException e) {
    	    //Log.e(TAG, "Could not write file " + e.getMessage());
    	}
    	*/
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        CONTEXT = this;
    }
 
    protected void onResume() {
        super.onResume();
        if (AccelerometerManager.isSupported()) {
            AccelerometerManager.startListening(this);
        }
    }
 
    protected void onDestroy() {
        super.onDestroy();
        if (AccelerometerManager.isListening()) {
            AccelerometerManager.stopListening();
        }
 
    }
 
    public static Context getContext() {
        return CONTEXT;
    }
 
    /**
     * onShake callback
     */
    public void onShake(float force) {
        Toast.makeText(this, "Phone shaked : " + force, 1000).show();
    }
 
    /**
     * onAccelerationChanged callback
     */
    public void onAccelerationChanged(float x, float y, float z) {
    	data.concat(Integer.toString((int)(x*100)) + " " 
    			+ Integer.toString((int)(y*100)) + " " 
    			+ Integer.toString((int)(z*100)) + "\n");
        ((TextView) findViewById(R.id.x)).setText(String.valueOf(x));
        ((TextView) findViewById(R.id.y)).setText(String.valueOf(y));
        ((TextView) findViewById(R.id.z)).setText(String.valueOf(z));
        time++;
        if(time == PERIOD){
        	WriteSettings(CONTEXT, data);
        }
    }


 // Save settings 
      public void WriteSettings(Context context, String data){ 
     	 try{
     		FileWriter fl = new FileWriter(f);
     		BufferedWriter bf = new BufferedWriter(fl);
     		bf.write(data);
     		bf.close();
     		//fl.close();
           Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
           } 
           catch (Exception e) {       
           e.printStackTrace(); 
           Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
           } 
           finally { 
        	   data = "";
        	   time = 0;
              /*try { 
                     f.close(); 
                     //fOut.close(); 
                     } catch (IOException e) { 
                     e.printStackTrace(); 
                     }
                  */ 
           } 
      }
}
